/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ufps.cuatro.util.render;

import com.ufps.cuatro.modelo.representacion.componente.Componente;
import com.ufps.cuatro.modelo.representacion.componente.ComponenteDistribuidor;
import com.ufps.cuatro.util.Utileria;
import java.awt.Color;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;

/**
 *
 * @author Carlos A. Torres C. 
 */
public class MyTableCellRendererDistribucionComponentes extends MyLabelRenderer{
    private static final long serialVersionUID = 5090358202207741125L;
    
    private ComponenteDistribuidor objComponenteDistribuidor;

    public MyTableCellRendererDistribucionComponentes(ComponenteDistribuidor objComponenteDistribuidor) {
        this.objComponenteDistribuidor = objComponenteDistribuidor;
        this.setHorizontalAlignment(CENTER);
        this.setVerticalAlignment(CENTER);
    }

    public MyTableCellRendererDistribucionComponentes(String text) {
        super(text);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        try{
            if(value instanceof String){
                this.setIcono("", (String) value, isSelected);                        
                return this;
            }
            if(value == null){
                this.setIcono("", "", isSelected);                        
                return this;
            }
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }catch(java.lang.Exception ex){}
        return null;
    }
    
    private void setIcono(String key, String info, boolean seleccionado){
        if(!key.isEmpty()){
            ImageIcon icono = Utileria.leerRecursoImagen(key, 16);
            JLabel labelIcono = new JLabel(icono);
            this.setIcon(labelIcono.getIcon());
        }                
        if(seleccionado){
            this.setOpaque(true);
            this.setForeground(Color.WHITE);
            this.setBackground(Color.BLACK);
            this.setText("<html><h4>" + info + "</h4></html>");
            this.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        }
        else {
            if(info.isEmpty()){
                this.setForeground(Color.LIGHT_GRAY);
                this.setBackground(Color.WHITE);                
                this.setText("<html><h5>Libre</h5></html>");
            }
            else{
                Componente objComponente = this.objComponenteDistribuidor.buscarComponenteEnDistribucion(info);
                this.setOpaque(true);
                this.setForeground(new Color(0,0,0));
                this.setBackground(objComponente.objColor);
                this.setText("<html><i><h5>" + info + "</h5></i></html>");
                this.setBorder(javax.swing.BorderFactory.createEtchedBorder());
            }
        }         
    }    
}
